﻿import com.mochimedia.ui.*;

/**
 * ...
 * @author Geoff
 */
class com.mochimedia.ui.ASUIObject extends Cell {
    
    private var _container:Cell;
    
    public function get root():Cell { return this; }
    
    private var _idMap:Object;
    public function get idMap():Object { return _idMap; }

    private var _nameMap:Object;
    public function get nameMap():Object { return _nameMap; }
	
    private var _form:Object;
    public function get form():Object { return _form; }
    
	private var _xmlString:String;
	private var _css:Object;
	
    //
    //
    public function ASUIObject (container:MovieClip, width:Number, height:Number, xmlString:String, css:Object, position:Position, style:Style) {
        
        init (container, width, height, xmlString, css, position, style);
        
        if (_container != null) create();
        
    }
    
    //
    //
    private function init (container:MovieClip, width:Number, height:Number, xmlString:String, css:Object, position:Position, style:Style):Void {
        
        super.init(container, width, height, false, false, 0, position, style);
        
        _idMap = { };
		_nameMap = { };
        _form = { };
        
        _width = (width > 0) ? width : (_container._width > 0) ? _container._width : Stage.width;
        _height = (height > 0) ? height : (_container._height > 0) ? _container._height : Stage.height; 
        
		_xmlString = xmlString;
		_css = css;
        
    }
    
    //
    //
    private function create ():Void {
        
		super.create();

        ASUIML.create(this, _xmlString, _css, _style);
		
    }
    
    //
    //
    public function connect (c:Component):Void {
        
        if (c.name.length > 0) {
            form[c.name] = (c.value != undefined) ? c.value : "";
            c.addEventListener(Component.EVENT_CHANGE, this, onEvent);
			c.addEventListener(Component.EVENT_CLICK, this, onEvent);
        }
        
    }
    
    //
    //
	public function connectHTMLField (h:HTMLField):Void {
		
		var self:Object = this;
		h.addEventListener(Component.EVENT_CLICK, this, onEvent);

	}
    
    //
    //
    public function onEvent (e:Event):Void {
        
        var c:Component = Component(e.target);
        
        if (c.name.length > 0) form[c.name] = c.value;
 
        bubble(e);
        
    }
	
    //
    //
    private function bubble (e:Event):Void {
        
        var listeners:Array = _listeners[e.type];

        if (listeners != undefined && listeners.length > 0) {
            
            for (var i:Number = listeners.length - 1; i >= 0; i--) {
                
                var e2:Event = Event(listeners[i]);
                if (e2.callbackObj == undefined) listeners.splice(i, 1);
                e2.dispatch(e.target);
        
            }
 
        }
        
    }
    
}